package com.hltx.controller.app.user;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.hltx.controller.base.BaseController;
import com.hltx.pojo.business.BusinessInfo;
import com.hltx.pojo.common.Page;
import com.hltx.pojo.group.Group;
import com.hltx.pojo.smscode.SmsCodeValidate;
import com.hltx.pojo.user.SMessage;
import com.hltx.pojo.user.User;
import com.hltx.pojo.user.UserAccount;
import com.hltx.service.order.OrderManager;
import com.hltx.service.picture.PictureManager;
import com.hltx.service.smscode.impl.SmsCodeValidateService;
import com.hltx.service.user.UserManager;
import com.hltx.util.CustomUtil;
import com.hltx.util.DateUtil;
import com.hltx.util.HttpClientManager;
import com.hltx.util.MessagesCode;
import com.hltx.util.PicUtil;
import com.hltx.util.RegexUtils;
import com.hltx.util.ResultInfo;
import com.hltx.util.sms.code.NeteaseSms;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/app/user/")
public class AppUserController extends BaseController {

	@Resource(name = "userService")
	private UserManager userService;
	
	@Value("${domain}")
	private String domain;

	@Resource(name = "smsCodeValidateService")
	private SmsCodeValidateService smsCodeValidateService;

	@Resource(name = "PictureService")
	private PictureManager PictureService;
	
	@Resource(name="orderService")
	private OrderManager orderService;

	// app用户信息
	@RequestMapping(value = "userCenter", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> appUser(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "用户信息");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String idStr = request.getParameter("id");
		Integer id = null;
		if (idStr != null && !"".equals(idStr)) {
			id = Integer.parseInt(idStr);
			try {
				User user = userService.getAppUser(id);
				
				if (user != null) {
					if("01".equals(user.getIs_business())){
						String bid = userService.getBusinessId(id);
						BusinessInfo bi = userService.getBusiness(bid);
						if(bi != null){
							resMap.put("merchant", bi);
						}
					}
					user.setImageAddress(user.getImageAddress());
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "查找成功"));
					resMap.put("user", user);
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "用户不存在!"));
				}
				Page p = new Page();
				p.getParams().put("userid",user.getId());
				Map<String, Object> stateCount=orderService.getStateCount(p);
				resMap.put("stateCount", stateCount);

			} catch (Exception e) {
				e.printStackTrace();
				this.logError(logger, e.getMessage());
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
			}
		} else {
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.PARAMS_ERROR, "参数错误"));
		}
		return resMap;
	}

	// app用户信息
	@RequestMapping(value = "userAlter", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> appUpdateUser(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "用户修改");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String idStr = request.getParameter("id");
		String petName = request.getParameter("petName");
		String sex = request.getParameter("sex");
		String address = request.getParameter("address");
		String birthday = request.getParameter("birthday");
		Integer id = null;
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}
		User user = new User();
		user.setId(id);
		user.setAddress(address);
		user.setBirthday(birthday);
		user.setSex(sex);
		user.setPetName(petName);
		try {
			if (user != null) {
				userService.updateAppUser(user);
				User ser = userService.getAppUser(id);
				resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "修改成功"));
				resMap.put("user", ser);
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		return resMap;
	}

	// 发送短信验证码
	@RequestMapping(value = "getCode", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> appSend(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "发送短信验证码");
		Map<String, Object> resMap = new HashMap<String, Object>();

		String mobile = request.getParameter("phone");
		if (mobile != null) {
			logBefore(logger, mobile);
			// 发送短信
			SmsCodeValidate sms = new SmsCodeValidate();
			sms.setPhone(mobile);
			sms.setTypes(request.getParameter("types"));
			try {
				SmsCodeValidate validate = smsCodeValidateService.getSmsData(sms);

				if (validate == null) {
					//Long nowTime = System.currentTimeMillis();
					sms.setExpireTime("60");
					String code = CustomUtil.buildRandom(6) + "";
					sms.setCode(code);
					sms.setSendNum(0);
					resMap.put("code", code);
					int i = smsCodeValidateService.saveSmsCode(sms);
					if (i >= 1) {
						resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "60"));
					} else {
						resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "短信入库异常"));
					}

				} else {
					if (Long.valueOf(validate.getExpireTime()) < System.currentTimeMillis()) {
						Long IntervalTime = (long) (validate.getSendNum() < 2 ? 60 : 1800);
						sms.setExpireTime(IntervalTime.toString());
						String code = CustomUtil.buildRandom(6) + "";
						sms.setCode(code);
						smsCodeValidateService.updateSmsCode(sms);
						resMap.put("code", code);
						resMap.put("result",
								this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, IntervalTime.toString()));
					} else {
						resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, " "));
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				this.logError(logger, e.getMessage());
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
			}
		} else {
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.HPONE_ISNULL, "手机号码不能为空!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	// 密码修改
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "forgetPwd", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> appUpdatePwd(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "修改密码");
		String pwd = request.getParameter("pwd");
		String phone = request.getParameter("phone");
		String code = request.getParameter("code");
		String confirmPwd = request.getParameter("confirmPwd");
		Map<String, Object> resMap = new HashMap<String, Object>();
		/*pwd = MD5Util.MD5(pwd);*/
		/*confirmPwd = MD5Util.MD5(confirmPwd);*/
		User user = new User();
		user.setPwd(pwd);
		user.setPhone(phone);
		user.setUserName(phone);
		user.setTypes("password");
		try {
			if (validateCode(request)) {
				if (pwd.equals(confirmPwd)) {
					String co = userService.getAppCode(user);
					if (code.equals(co)) {
						User us = userService.getLoginId(user);
						if (us != null) {
							Map<String, Object> nmap = new HashMap<>();
							nmap.put("newpassword", pwd);
							user.setPwd(us.getPwd());
							String hxId = userService.getHxId(user);
							if (hxId != null) {
								String massage = HttpClientManager.updateHxPwd(hxId, nmap);
								JSONObject jsonO = JSONObject.fromObject(massage);
								Map<String, Object> mapJson = JSONObject.fromObject(jsonO);
								String action = (String) mapJson.get("action");
								if (action != null && action != "") {
									user.setPwd(pwd);
									userService.updateAppPwd(user);
									//String newPwd = userService.getPwd(user);
									resMap.put("result",
											this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "密码修改成功"));
								} else {
									resMap.put("result",
											this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "环信重置密码失败!"));
								}
							} else {
								resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "用户不存在!"));
							}
						} else {
							resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "用户不存在!"));
						}
					}
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.RESET, "两次密码输入不匹配!"));
				}
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.TEST, "验证码有误!"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	// 修改密码验证
	@RequestMapping(value = "forCode", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> appForgetPwdCode(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.logBefore(logger, "验证码");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			String Code = request.getParameter("code");
			String phone = request.getParameter("phone");
			String type = request.getParameter("type");
			User user = new User();
			user.setPhone(phone);
			user.setTypes(type);
			SmsCodeValidate sv = new SmsCodeValidate();
			sv.setPhone(phone);
			sv.setTypes(type);
			sv.setCode(Code);
			sv.setIsUse("01");
			long lose = 0;
			String code = userService.getAppCode(user);
			if (Code.equals(code)) {
				SmsCodeValidate date = smsCodeValidateService.getSmsCode(sv);
				if ("00".equals(date.getIsUse())) {
					String time = date.getExpireTime();
					if (time != null) {
						lose = Long.parseLong(time);
					}
					Long newTime = System.currentTimeMillis();
					if (date.getSendNum() > 5) {
						resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MAX_SEND, "今日验证码信息已用完!"));
					} else {
						if (lose < (newTime / 1000)) {
							resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.TEST, "验证码失效!"));
						} else {
							resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "验证通过"));
							smsCodeValidateService.upudateIsSue(sv);
						}
					}
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.TEST, "验证码无效!"));
				}
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.TEST, "验证码有误!"));
			}
		} catch (Exception e) {
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	// 手机修改
	@RequestMapping(value = "updatePhone", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> appUpdatePhone(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> params) throws Exception {
		this.logBefore(logger, "修改手机");
		Map<String, Object> resMap = new HashMap<String, Object>();
		Integer id = null;
		try {
			String ids = request.getParameter("id");
			String Code = request.getParameter("code");
			String phone = request.getParameter("phone");
			String type = request.getParameter("type");
			if (ids != null) {
				id = Integer.parseInt(ids);
			}
			User user = new User();
			user.setPhone(phone);
			user.setTypes(type);
			user.setUserName(phone);
			User loginId = userService.getLoginId(user);
			if (loginId == null) {
				if (validateCode(request)) {
					String code = userService.getAppCode(user);
					if (Code.equals(code)) {
						userService.updatePhone(id, phone);
						User use = userService.getAppUser(id);
						resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "修改成功"));
						resMap.put("user", use);
					} else {
						resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.TEST, "验证码有误!"));
					}
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.TEST, "验证码失效!"));
				}
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.HAVE, "用户已存在!"));
			}
		} catch (Exception e) {
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
			return resMap;
		}
		this.logAfter(logger);
		return resMap;
	}

	// 验证码验证
	private boolean validateCode(HttpServletRequest request) {
		String phone = request.getParameter("phone");
		String code = request.getParameter("code");
		String types = request.getParameter("type");
		SmsCodeValidate sv = new SmsCodeValidate();
		sv.setPhone(phone);
		sv.setTypes(types);
		sv.setCode(code);
		long lose = 0;
		try {
			SmsCodeValidate date = smsCodeValidateService.getSmsCode(sv);
			String time = date.getExpireTime();
			if (time != null) {
				lose = Long.parseLong(time);
			}
			Long newTime = System.currentTimeMillis();
			if (lose > (newTime / 1000) && date.getSendNum() < 3) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	// @RequestMapping(value ="addFriend",method = { RequestMethod.POST})
	// @ResponseBody
	// public Map<String, Object> appAddFriend(HttpServletRequest
	// request,HttpServletResponse response)throws Exception {
	// this.logBefore(logger, "添加好友");
	// Map<String, Object> resMap = new HashMap<String, Object>();
	// try{
	// String friendName = request.getParameter("phone");
	// String userName = request.getParameter("phone");
	// User user = new User();
	// user.setPhone(friendName);
	// String us = userService.getUserByPhone(user);
	// if(us != null){
	// String myName = "hltx"+MD5Util.MD5(userName);
	// String friend = "hltx"+MD5Util.MD5(friendName);
	// String friendData = HttpClientManager.addFriend(myName,friend);
	// if(friendData != null && friendData !=""){
	// user.setHxId(friend);
	// String frienguser = userService.getLoginName(user);
	// resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
	// resMap.put("friendAccount",frienguser);
	// }else{
	// resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.LOSE,
	// "好友添加失败!"));
	// }
	// }else{
	// resMap.put("result", this.initResult(ResultInfo.ERROR,
	// MessagesCode.EXIST, "用户不存在!"));
	// }
	// }catch(Exception e){
	// this.logError(logger, e.getMessage());
	// resMap.put("result", this.initResult(ResultInfo.ERROR,
	// MessagesCode.SYSTEM, "系统繁忙!"));
	// return resMap;
	// }
	// this.logAfter(logger);
	// return resMap;
	// }
	// @RequestMapping(value ="chatMessage",method = { RequestMethod.POST})
	// @ResponseBody
	// public Map<String, Object> appChatMessage(HttpServletRequest
	// request,HttpServletResponse response)throws Exception {
	// this.logBefore(logger, "发送信息");
	// Map<String, Object> resMap = new HashMap<String, Object>();
	// Map<String, Object> map = new LinkedHashMap<String, Object>();
	// try{
	// String friendName = request.getParameter("phone");
	// String userName = request.getParameter("phone");
	// String message = request.getParameter("message");
	// User user = new User();
	// user.setPhone(friendName);
	// String us = userService.getUserByPhone(user);
	// if(us != null){
	// String myName = "hltx"+MD5Util.MD5(userName);
	// String friend = "hltx"+MD5Util.MD5(friendName);
	// String[] target = new String[]{friend};
	// map.put("target_type", "users");
	// map.put("target", target);
	// map.put("msg", map.put("type", "txt"));
	// map.put("msg", map.put("msg", "message"));
	// map.put("from", myName);
	// String friendData = HttpClientManager.addFriend(myName,friend);
	// if(friendData != null && friendData !=""){
	// user.setHxId(friend);
	// String frienguser = userService.getLoginName(user);
	// resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
	// resMap.put("friendAccount",frienguser);
	// }else{
	// resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.LOSE,
	// "好友添加失败!"));
	// }
	// }else{
	// resMap.put("result", this.initResult(ResultInfo.ERROR,
	// MessagesCode.EXIST, "用户不存在!"));
	// }
	// }catch(Exception e){
	// this.logError(logger, e.getMessage());
	// resMap.put("result", this.initResult(ResultInfo.ERROR,
	// MessagesCode.SYSTEM, "系统繁忙!"));
	// return resMap;
	// }
	// this.logAfter(logger);
	// return resMap;
	// }
	// 好友列表
	@RequestMapping(value = "friendsList", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> appFriendsList(HttpServletResponse response, HttpServletRequest request, Page page) {
		this.logBefore(logger, "好友列表");
		String userId = request.getParameter("id");
		Map<String, Object> resMap = new HashMap<String, Object>();
		User use = new User();
		try {
			if (userId != null) {
				int start = (page.getPage() - 1) * page.getShowNumber();
				use.setId(Integer.parseInt(userId));
				use.setStart(start);
				List<User> user = userService.getFriendsAll(use);
				resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "查找成功"));
				resMap.put("user", user);
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	// 添加好友
	@RequestMapping(value = "addFirend", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> addAppFirend(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "添加好友");
		String phone = request.getParameter("phone");
		String userId = request.getParameter("id");
		Map<String, Object> resMap = new HashMap<String, Object>();
		int myId = 0;
		User user = new User();
		if (userId != null) {
			myId = Integer.parseInt(userId);
		}
		user.setId(myId);
		// int id;
		try {
			if (phone != null && !"".equals(phone)) {
				user.setPhone(phone);
				String us = userService.getUserByPhone(user);
				if (us != null) {
					int id = Integer.parseInt(us);
					user.setFirendId(id);
					String number = userService.getFirend(user);
					if (number == null) {
						userService.saveAppFirend(user);
						userService.saveAppFirends(user);
						resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "添加成功"));
					} else {
						resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.HAVE, "该用户已是您的好友"));
					}
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "用户不存在!"));
				}
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "请填写手机号!"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	// 设置群信息
	@RequestMapping(value = "groupMsg", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> appGroupMsg(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "设置群信息");
		String groupId = request.getParameter("groupId");
		String groupName = request.getParameter("groupname");
		String greferral = request.getParameter("greferral");
		String gnotice = request.getParameter("gnotice");
		// String member = request.getParameter("member");
		String state = request.getParameter("state");
		String qunzhu = request.getParameter("qunzhu");
		Map<String, Object> resMap = new HashMap<String, Object>();
		int id = 0;
		int qzid = 0;
		Group group = new Group();
		if (groupId != null) {
			id = Integer.parseInt(groupId);
		}
		if (qunzhu != null) {
			qzid = Integer.parseInt(qunzhu);
		}
		group.setId(id);
		group.setGroupName(groupName);
		group.setGnotice(gnotice);
		group.setGreferral(greferral);
		// group.setMember(Integer.parseInt(member));
		group.setState(state);
		group.setQZID(qzid);
		try {
			if (group != null) {
				String qz = userService.getByQZ(group);
				if (qz != null) {
					userService.updateGroupMsg(group);
					Group gro = userService.getGroupMsg(group);
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, ""));
					resMap.put("groupMsg", gro);
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.nickname, "权限不够!"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	// 创建群聊
	@RequestMapping(value = "appGroup", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> saveAppGroup(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "创建群聊");
		String groupName = request.getParameter("groupname");
		String secure = request.getParameter("secure");
		String qunzhu = request.getParameter("qunzhu");
		Map<String, Object> resMap = new HashMap<String, Object>();
		int qzid = 0;
		int id = 0;
		Group group = new Group();
		group.setGroupName(groupName);
		group.setSecure(secure);
		if (qunzhu != null) {
			qzid = Integer.parseInt(qunzhu);
			group.setQZID(qzid);
			try {
				if (group != null) {
					String GR = userService.sgetGroupName(group);
					if (GR == null) {
						id = userService.saveAppGroup(group);
						group.setId(id);
						if (id > 0) {
							Group gro = userService.getGroupMsg(group);
							resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "创建成功"));
							resMap.put("groupMsg", gro);
						} else {
							resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.LOSE, "创建失败!"));
						}
					} else {
						resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.nickname, "昵称重复!"));
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				this.logError(logger, e.getMessage());
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
			}
		}
		this.logAfter(logger);
		return resMap;
	}

	// 群聊成员
	@RequestMapping(value = "groupMember", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> appGroupMember(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "群聊成员");
		String groupId = request.getParameter("groupId");
		Map<String, Object> resMap = new HashMap<String, Object>();
		int id = 0;
		Group group = new Group();
		if (groupId != null) {
			id = Integer.parseInt(groupId);
			group.setId(id);
			try {
				List<User> user = userService.getGroupMember(group);
				resMap.put("groupMember", user);
			} catch (Exception e) {
				e.printStackTrace();
				this.logError(logger, e.getMessage());
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
			}
		}
		this.logAfter(logger);
		return resMap;
	}

	// app头像修改
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "groupImage", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> groupImage(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "头像修改");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String idStr = request.getParameter("groupId");
		Integer id = null;
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		if (multipartResolver.isMultipart(request)) {
			System.out.println("ss");
		}
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multipartRequest.getFile("file");
		InputStream in = null;
		try {
			in = file.getInputStream();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		Group group = new Group();
		group.setId(id);
		try {
			group = userService.getGroupMsg(group);
			String path = "/personal" + "/" + CustomUtil.buildRandom(4) + id;
			Map imagePath = PictureService.uploadPictureFile(in, path, "groupHead.png");
			String imageAddress = (String) imagePath.get("url");
			userService.updateimageAddress(id, imageAddress);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		return resMap;
	}

	// 根据手机查找用户
	@RequestMapping(value = "getByPhone", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getByPhone(HttpServletResponse response, HttpServletRequest request, User user) {
		this.logBefore(logger, "根据手机查找用户");
		String phone = request.getParameter("phone");
		String petName = request.getParameter("petName");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (phone != null && !"".equals(phone)) {
				int start = (user.getPage() - 1) * user.getShowNumber();
				user.setPhone(phone);
				user.setStart(start);
				List<User> us = userService.getByPhone(user);
				if (us.size() > 0) {
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "查找成功"));
					resMap.put("user", us);
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "用户不存在!"));
				}
			}
			if (petName != null && !"".equals(petName)) {
				int start = (user.getPage() - 1) * user.getShowNumber();
				user.setPetName(petName);
				user.setStart(start);
				List<User> use = userService.getByName(user);
				if (use.size() > 0) {
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "查找成功"));
					resMap.put("user", use);
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "用户不存在!"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}

		this.logAfter(logger);
		return resMap;
	}

	// 根据环信id查找用户
	@RequestMapping(value = "getByHxId", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getByHxId(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "根据环信id查找用户");
		String hxId = request.getParameter("hxId");
		Map<String, Object> resMap = new HashMap<String, Object>();
		User user = new User();
		if (hxId != null && !"".equals(hxId)) {
			user.setHxId(hxId);
			try {
				User us = userService.getById(user);
				if (us != null && us.getPhone() != null) {
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "查找成功"));
					resMap.put("petName", us.getPetName());
					resMap.put("headImage", us.getImageAddress());
					resMap.put("phone", us.getPhone());
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "用户不存在!"));
				}
			} catch (Exception e) {
				e.printStackTrace();
				this.logError(logger, e.getMessage());
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
			}
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "send", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> send(HttpServletResponse response,HttpServletRequest request){
		this.logBefore(logger, "发送短信验证码");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String mobile = request.getParameter("userName");
		try{
			if(mobile != null){
				logBefore(logger, mobile);
				if(RegexUtils.isMobileNO(mobile)){
					//发送短信
					SmsCodeValidate sms = new SmsCodeValidate();
					sms.setPhone(mobile);
					
					String types =request.getParameter("type");
					sms.setTypes(types);
					SmsCodeValidate validate = smsCodeValidateService.getSmsData(sms);
					if (types != null) {
						Integer is = userService.userIsExists(mobile);
						if("register".equals(types)){
							 if(is > 0){
								 resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.HAVE, "此手机号已被注册"));
							 }else{
								 	if(validate ==null){
								 		sms.setExpireTime("120");
										String code =CustomUtil.buildRandom(6)+"";
										sms.setCode(code);
										sms.setSendNum(0);
										String msg = sendDB(sms, true);
										if (msg == "success") {
											resMap.put("result", this.initResult(ResultInfo.SUCCESS, code, "已发送短信"));
										}
								 	}else{
								 		sms.setExpireTime("60");
										String code =CustomUtil.buildRandom(6)+"";
										sms.setCode(code);
										sms.setSendNum(0);
										String msg = sendDB(sms, false);
										if (msg == "success") {
											resMap.put("result", this.initResult(ResultInfo.SUCCESS, code, "已发送短信"));
										}
								 	}
								 	
							 }
						}else{
							if(is<=0){
								resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "此手机号不存在"));
							}else{
								if (validate ==null) {
									sms.setExpireTime("120");
									String code =CustomUtil.buildRandom(6)+"";
									sms.setCode(code);
									sms.setSendNum(0);
									String msg = sendDB(sms, true);
									if (msg == "success") {
										resMap.put("result", this.initResult(ResultInfo.SUCCESS, code, "已发送短信"));
									}
								}else{
									sms.setTypes(request.getParameter("type"));
									SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); 
									String date = format.format(new Date());
									String date_time= validate.getCreateTime();
									date_time = date_time.substring(0, 10).replace("-", "").trim();
									System.out.println(date+"==="+date_time);
									if(!date.equals(date_time)){
										validate.setSendNum(-1);
										smsCodeValidateService.intiSmsData(validate);
										sms.setExpireTime("120");
										String code =CustomUtil.buildRandom(6)+"";
										sms.setCode(code);
										sms.setSendNum(0);
										String msg = sendDB(sms, false);
										if (msg == "success") {
											resMap.put("result", this.initResult(ResultInfo.SUCCESS, code, "已发送短信"));
										}
									}else{
										if(validate.getSendNum()>=10){
											resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.ERROR, "获取验证码已经达到上线请在次日后再试"));
											//
										}else{
											long  now = System.currentTimeMillis();
											long  dates = Long.valueOf(DateUtil.dateToStamp(validate.getCreateTime())).longValue();
											if(validate != null && validate.getSendNum() < 3){
												sms.setExpireTime("120");
												String code =CustomUtil.buildRandom(6)+"";
												sms.setCode(code);
												sms.setSendNum(0);
												String msg = sendDB(sms, false);
												if (msg == "success") {
													resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "已发送短信"));
												}
											}else{
												if(validate != null &&  validate.getSendNum() >= 3 && ((now/1000) - (dates/1000)) > 900){//发送次数大于3次并且上次生成验证码时间间隔大于15分钟
													sms.setExpireTime("120");
													String code =CustomUtil.buildRandom(6)+"";
													sms.setCode(code);
													sms.setSendNum(0);
													String msg = sendDB(sms, false);
													if (msg == "success") {
														resMap.put("result", this.initResult(ResultInfo.SUCCESS, code, "已发送短信"));
													}
												}else{
													resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.ERROR, "发送验证码过于频繁请在15分钟后再试"));
												}
											}
										}
										
									}
								}
								
							}
							
						}
					}else{
						resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "缺少类型参数"));
					}
				}else{
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.ERROR, "手机号码不合法!"));
				}
			}else{
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.HPONE_ISNULL, "手机号码不能为空!"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "deleteFirend", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> deleteFirend(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "删除好友");
		String id = request.getParameter("id");
		String firendId = request.getParameter("firendId");
		Map<String, Object> resMap = new HashMap<String, Object>();
		User user = new User();
		try {
			if (firendId != null && id != null) {
				user.setId(Integer.parseInt(id));
				user.setFirendId(Integer.parseInt(firendId));
				int num = userService.deleteFirend(user);
				if (num > 0) {
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "删除成功"));
					userService.deleteApply(user);
				}
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.PARAMS_ERROR, "参数错误"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "getPay", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getPay(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "支付密码");
		String id = request.getParameter("id");
		//String pay = request.getParameter("payPwd");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (id != null) {
				User payPwd = userService.getPayPwd1(id);
				if (payPwd.getPayPwd() != null) {
					resMap.put("state", "1");
				} else {
					resMap.put("state", "0");
				}
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.PARAMS_ERROR, "参数错误"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "updatePayPwd", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> updatePayPwd(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "支付密码");
		String id = request.getParameter("id");
		String pay = request.getParameter("payPwd");
		String type = request.getParameter("type");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (type != null && !"".equals(type)) {
				if (validateCode(request)) {
					User payPwd = userService.getPayPwd1(id);
					if (pay != payPwd.getPayPwd()) {
						userService.updatePayPwd(payPwd.getLoginId(), pay);
						resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "设置成功"));
					} else {
						resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.PWD, "支付密码未改动"));
					}

				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.TEST, "验证码无效"));
				}
			} else {
				User payPwd = userService.getPayPwd1(id);
				if (pay != payPwd.getPayPwd()) {
					userService.updatePayPwd(payPwd.getLoginId(), pay);
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "设置成功"));
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.PWD, "支付密码未改动"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "getApplyState", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getApplyState(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "申请添加好友");
		String id = request.getParameter("id");
		String firendId = request.getParameter("firendId");
		Map<String, Object> resMap = new HashMap<String, Object>();
		User user = new User();
		try {
			if (id != null && firendId != null) {
				user.setId(Integer.parseInt(id));
				user.setFirendId(Integer.parseInt(firendId));
				String number = userService.getFirend(user);
				if (number == null) {
					int nu = userService.getApplyFirend(user);
					if (nu < 1) {
						int num = userService.saveApply(user);
						if (num > 0) {
							resMap.put("state", "1");
						}
					} else {
						resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.HAVE, "该用户已申请"));
					}
				} else {
					resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.HAVE, "该用户已是您的好友"));
				}
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.PARAMS_ERROR, "参数错误"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "getFirendApply", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getFirendApply(HttpServletResponse response, HttpServletRequest request, Page page) {
		this.logBefore(logger, "申请添加好友");
		String id = request.getParameter("id");
		Map<String, Object> resMap = new HashMap<String, Object>();
		User use = new User();
		try {
			if (id != null) {
				int start = (page.getPage() - 1) * page.getShowNumber();
				use.setId(Integer.parseInt(id));
				use.setStart(start);
				List<User> user = userService.getApply(use);
				int s = 0;
				for (User us : user) {
					if ("0".equals(us.getApplyState())) {
						s = +1;
					}
				}
				if (user.size() > 0) {
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "查找成功"));
					resMap.put("user-size", user.size());
					resMap.put("apply-size", s);
					resMap.put("user", user);
				} else {
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.EXIST, "没有好友申请"));
				}
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.PARAMS_ERROR, "参数错误"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "saveFirendApply", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> saveFirendApply(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "申请添加好友");
		String id = request.getParameter("id");
		String firendId = request.getParameter("firendId");
		String state = request.getParameter("state");
		Map<String, Object> resMap = new HashMap<String, Object>();
		User user = new User();
		try {
			if (id != null && firendId != null) {
				user.setId(Integer.parseInt(id));
				user.setFirendId(Integer.parseInt(firendId));
				if ("1".equals(state)) {
					String number = userService.getFirend(user);
					if (number == null) {
						userService.saveAppFirend(user);
						userService.saveAppFirends(user);
						userService.updateApply(user);
						resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "添加成功"));
					} else {
						resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.HAVE, "该用户已是您的好友"));
					}
				} else {
					userService.deleteApply(user);
				}
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.PARAMS_ERROR, "参数错误"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	// app头像修改
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "saveHeadImage", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> appHeadImage(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "头像修改");
		Map<String, Object> resMap = new HashMap<String, Object>();
		String idStr = request.getParameter("id");
		Integer id = null;
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		if (multipartResolver.isMultipart(request)) {
			System.out.println("ss");
		}
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multipartRequest.getFile("file");
		InputStream in = null;
		try {
			in = PicUtil.cutImage(file.getInputStream(), 200, 200);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		User user = new User();
		user.setId(id);
		try {
			user = userService.getUser(user);
			String codePre = user.getCodePre();
			String codeSuf = user.getCodeSuf();
			String path = "/personal" + "/" + codePre + id + codeSuf;
			Map imagePath = PictureService.uploadPictureFile(in, path, "userHead.png");
			String imageAddress = (String) imagePath.get("url");
			imageAddress = imageAddress + "?v" + CustomUtil.buildRandom(4);
			userService.uploadimageAddress(id, imageAddress);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", "头像修改成功"));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		return resMap;
	}

	@RequestMapping(value = "getSystemMessage", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> systemMessage(HttpServletResponse response, HttpServletRequest request, User user) {
		this.logBefore(logger, "系统信息");
		String id = request.getParameter("id");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			int start = (user.getPage() - 1) * user.getShowNumber();
			user.setStart(start);
			user.setId(Integer.parseInt(id));
			List<SMessage> message = userService.getMessage(user);
			int s = 0;
			for (SMessage sMessage : message) {
				if ("0".equals(sMessage.getMessageState())) {
					s += 1;
				}
			}
			if (message.size() > 0) {
				resMap.put("message-size", message.size());
				resMap.put("unread-size", s);
				resMap.put("message", message);
			} else {
				resMap.put("result", this.initResult(ResultInfo.ERROR, "", "没有未读信息!"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "setMessage", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> updateMessage(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "设置系统信息");
		String id = request.getParameter("id");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			userService.updateMessage(id);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "设置成功!"));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	private String sendDB(SmsCodeValidate sms, Boolean o) {
		try {
			int i = 0;
			if (o) {
				sms.setIsUse("00");
				i = smsCodeValidateService.saveSmsCode(sms);
			} else {
				sms.setIsUse("00");
				i = smsCodeValidateService.updateSmsCode(sms);
			}
			if (i >= 1) {
				NeteaseSms.sendTemplateSms(sms.getPhone(), sms.getCode());
			}
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			return "error";
		}
	}

	@RequestMapping(value = "getMessageSize", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getMessageSize(HttpServletResponse response, HttpServletRequest request, Page page) {
		this.logBefore(logger, "系统信息");
		String id = request.getParameter("id");
		Map<String, Object> resMap = new HashMap<String, Object>();
		User use = new User();
		try {
			if (id != null) {
				int start = (page.getPage() - 1) * page.getShowNumber();
				use.setId(Integer.parseInt(id));
				use.setStart(start);
				List<User> user = userService.getApply(use);
				int s = 0;
				for (User us : user) {
					if ("0".equals(us.getApplyState())) {
						s += 1;
					}
				}
				if (user.size() > 0) {
					resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "查找成功"));
					resMap.put("apply-size", s);
				}
				List<SMessage> message = userService.getMessage(use);
				int n = 0;
				for (SMessage sMessage : message) {
					if ("0".equals(sMessage.getMessageState())) {
						n += 1;
					}
				}
				if (message.size() > 0) {
					resMap.put("message-size", message.size());
					resMap.put("unread-size", n);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "getCodeMessage", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getCodeMessage(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "设置二维码code");
		String id = request.getParameter("id");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (id != null && !"".equals(id)) {
				resMap.put("code", CustomUtil.buildRandom(3) + id + CustomUtil.buildRandom(4));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "getScanCode", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getScanCode(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "解析二维码");
		String code = request.getParameter("code");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (code != null && !"".equals(code)) {
				String num = code.substring(3, code.length() - 4);
				User appUser = userService.getAppUser(Integer.parseInt(num));
				resMap.put("type", "addFriend");
				resMap.put("user", appUser);
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}

	@RequestMapping(value = "getAccount", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getAccount(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "获取账户信息");
		String userId = request.getParameter("id");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (userId != null && !"".equals(userId)) {
				UserAccount ua = userService.getAccount(userId);
				resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "查找成功"));
				resMap.put("account", ua);
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	/**
	 * 升级VIP
	 */
	@RequestMapping("wap/myvip/toupgradevip")
	public String vip() {
		return "redirect:/user/myvip/toupgradevip";
	}
	/**
	 * 充值
	 */
	@RequestMapping("wap/myvip/goRecharge")
	public String recharge() {
		return "redirect:/user/myvip/goRecharge";
	}
	
	@RequestMapping(value = "getRegister", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getRegister(HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "获取注册");
		String codePre = request.getParameter("codePre");
		String userId = request.getParameter("id");
		String codeSuf = request.getParameter("codeSuf");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (userId != null && !"".equals(userId)) {
				String vipcode = codePre+userId+codeSuf;
				String href = domain+"/friend/toRegister?code="+vipcode;
				resMap.put("href", href);
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping("wap/regulation")
	public String friend() {
		return "redirect:/user/myvip/regulation";
	}

}
